home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / EAGLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-07  |  24.0 KB  |  793 lines

  1. /*
  2.  * Interface driver for the EAGLE board for KA9Q's TCP/IP on an IBM-PC ONLY!
  3.  *
  4.  *  Written by Art Goldman, WA3CVG - (c) Copyright 1987 All Rights Reserved
  5.  *  Permission for non-commercial use is hereby granted provided this notice
  6.  *  is retained.  For info call: (301) 997-3838.
  7.  *
  8.  *  10 Jan 88    ng6q    - Corrected IDLE comparison in doegstat.
  9.  *   6 Apr 88    ng6q    - Changed eg_raw to prevent calling egtxint with a
  10.  *              packet in sndbuf.  Initialized sndq and rcvq in
  11.  *              eg_attach.  Added carrier detect check before
  12.  *              slot time delay in egtxint.  Should make major
  13.  *              changes to egtxint to avoid delay loops while
  14.  *              masked for receive interrupts.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include "global.h"
  19. #include "mbuf.h"
  20. #include "iface.h"
  21. #include "eagle.h"
  22. #include "8530.h"
  23. #include "ax25.h"
  24. #include "trace.h"
  25. #include <time.h>
  26.  
  27. void eg0vec(),write_scc(),waitmsec(),doeg();
  28. int eg_init(),eg_stop(),ax_send(),ax_output(),eg_raw(),eg_ctl();
  29. void (*getirq())();
  30. extern struct iface *Ifaces;
  31. extern struct mbuf *Hopper;
  32. static void egrxint(),egtxint(),egexint(),rts();
  33.  
  34. struct EGTAB Eagle[EGMAX];    /* Device table - one entry per card */
  35. void (*eghandle[])() = { eg0vec };  /* handler interrupt vector table */
  36. struct egchan Egchan[2*EGMAX];       /* channel table - 2 entries per card */
  37. int16 Egnbr;
  38.  
  39. /* Master interrupt handler.  One interrupt at a time is handled.
  40.  * here. Service routines are called from here.
  41.  */
  42. void
  43. egint(dev)
  44. int16 dev;
  45. {
  46.     register char st;
  47.     register int16 pcbase;
  48.     struct egchan *hp;
  49.  
  50.     Eagle[dev].ints++;
  51.     pcbase = Eagle[dev].addr;
  52.  
  53.     /* Read interrupt status register from channel A */
  54.     while((st = read_scc(pcbase+CHANA+CTL,R3)) != 0) {
  55.         /* Use IFs to process ALL interrupts pending
  56.          * because we need to check all interrupt conditions
  57.          */
  58.         if (st & CHARxIP) {
  59.             /* Channel A Rcv Interrupt Pending */
  60.             hp = &Egchan[2 * dev];
  61.             egrxint(hp);
  62.         } else if (st & CHATxIP) {
  63.             /* Channel A Transmit Int Pending */
  64.             hp = &Egchan[2 * dev];
  65.             egtxint(hp);
  66.         } else if (st & CHAEXT) {
  67.             /* Channel A External Status Int */
  68.             hp = &Egchan[2 * dev];
  69.             egexint(hp);
  70.         } else if (st & CHBRxIP) {
  71.             /* Channel B Rcv Interrupt Pending */
  72.             hp = &Egchan[(2 * dev)+1];
  73.             egrxint(hp);
  74.         } else if (st & CHBTxIP) {
  75.             /* Channel B Transmit Int Pending */
  76.             hp = &Egchan[(2 * dev)+1];
  77.             egtxint(hp);
  78.         } else if (st & CHBEXT) {
  79.             /* Channel B External Status Int */
  80.             hp = &Egchan[(2 * dev)+1];
  81.             egexint(hp);
  82.         }
  83.         /* Reset highest interrupt under service */
  84.         write_scc(hp->base+CTL,R0,RES_H_IUS);
  85.     } /* End of while loop on int processing */
  86. }
  87.  
  88. /* Eagle SIO External/Status interrupts
  89.  * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
  90.  * Receiver automatically goes to Hunt on an abort.
  91.  *
  92.  * If the Tx Underrun interrupt hits, change state and
  93.  * issue a reset command for it, and return.
  94.  */
  95. static void
  96. egexint(hp)
  97. register struct egchan *hp;
  98. {
  99.     char st, i_state;
  100.  
  101.     i_state = dirps();        /* disable interrupts */
  102.     hp->exints++;
  103.     st = read_scc(hp->base+CTL,R0);     /* Fetch status */
  104.  
  105.     /* Check for Tx UNDERRUN/EOM - only in Transmit Mode */
  106.     if((hp->rstate==0) && (st & TxEOM)) {
  107.         /* if in UNDERRUN, go to FLAGOUT state
  108.          * see explanation under egtxint()
  109.          * CRC & FLAG now going out, so
  110.          * wait for Tx BUffer Empty int
  111.          */
  112.  
  113.         /* If we are not in underrun, this is an unexpected
  114.          * underrun.  EOM bit should be set, so the SCC will
  115.          * now send an abort
  116.          */
  117.  
  118.         if(hp->tstate == UNDERRUN)
  119.             hp->tstate = FLAGOUT;
  120.  
  121.         /* Tx Buff EMPTY interrupt occurs after CRC is sent */
  122.     }
  123.  
  124.     /* Receive Mode only
  125.      * This triggers when hunt mode is entered, & since an ABORT
  126.      * automatically enters hunt mode, we use that to clean up
  127.      * any waiting garbage
  128.      */
  129.     if((hp->rstate == ACTIVE) && (st & BRK_ABRT)) {
  130.         hp->rcp = hp->rcvbuf->data;
  131.         hp->rcvbuf->cnt = 0;          /* rewind on DCD transition */
  132.         hp->aborts++;              /* nbr aborts * 2 */
  133.     }
  134.  
  135.     /* reset external status latch */
  136.     write_scc(CTL+hp->base,R0,RES_EXT_INT);
  137.  
  138.     restore(i_state);
  139. }
  140.  
  141. /* EG receive interrupt handler. The first receive buffer is pre-allocated
  142.  * in the init routine.  Thereafter, it is filled here, queued out, and a
  143.  * new one acquired.  CRC, OVERRUN and TOOBIG errors merely 'rewind' the
  144.  * pointers and reuse the same buffer.
  145.  */
  146. static void
  147. egrxint(hp)
  148. register struct egchan *hp;
  149. {
  150.     register int16 base;
  151.     char rse, i_state;
  152.     struct mbuf *bp;
  153.     struct phdr *phdr;
  154.  
  155.     i_state = dirps();        /* disable interrupts */
  156.     hp->rxints++;
  157.     base = hp->base;
  158.  
  159.     if ((read_scc(base+CTL,R0)) & Rx_CH_AV) {
  160.         /* there is a char to be stored
  161.          * read special condition bits before reading the data char
  162.          */
  163.         rse = read_scc(hp->base+CTL,R1); /* get status byte from R1 */
  164.         if(rse & Rx_OVR) {
  165.             /* Rx overrun - toss buffer */
  166.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  167.             hp->rcvbuf->cnt = 0;
  168.             hp->rstate = RXERROR;    /* set error flag */
  169.             hp->rovers++;        /* count overruns */
  170.         } else if(hp->rcvbuf->cnt >= hp->bufsiz) {
  171.             /* Too large -- toss buffer */
  172.             hp->toobig++;
  173.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  174.             hp->rcvbuf->cnt = 0;
  175.             hp->rstate = TOOBIG;    /* when set, chars are not stored */
  176.         }
  177.         /* ok, we can store the received character now */
  178.         if(hp->rstate == ACTIVE) {        /* If no errors... */
  179.             *hp->rcp++ = inportb(base+DATA);    /* char to rcv buff */
  180.             hp->rcvbuf->cnt++;            /* bump count */
  181.         } else {
  182.             /* got to empty FIFO */
  183.             (void) inportb(base+DATA);
  184.             write_scc(hp->base+CTL,R0,ERR_RES);    /* reset err latch */
  185.             hp->rstate = ACTIVE;
  186.         }
  187.     }
  188.     /* char has been stored
  189.      * read special condition bits
  190.      */
  191.     rse = read_scc(hp->base+CTL,R1);     /* get status byte from R1 */
  192.  
  193.     /* The End of Frame bit is ALWAYS associated with a character,
  194.      * usually, it is the last CRC char.  Only when EOF is true can
  195.      * we look at the CRC byte to see if we have a valid frame
  196.      */
  197.     if(rse & END_FR) {
  198.         hp->rxframes++;
  199.         /* END OF FRAME -- Make sure Rx was active */
  200.         if(hp->rcvbuf->cnt > 0) {        /* any data to store */
  201.             /* looks like a frame was received
  202.              * now is the only time we can check for CRC error
  203.              */
  204.             if((rse & CRC_ERR) || (hp->rstate > ACTIVE) || (hp->rcvbuf->cnt < 10)) {
  205.                 /* error occurred; toss frame */
  206.                 if(rse & CRC_ERR)
  207.                     hp->crcerr++;    /* count CRC errs */
  208.                 if(hp->rstate == RXERROR)
  209.                     hp->rovers++;
  210.                 /* don't throw away buffer -
  211.                  * merely reset the pointers
  212.                  */
  213.                 hp->rcp = hp->rcvbuf->data;
  214.                 hp->rcvbuf->cnt = 0;
  215.             } else {
  216.                 /* Here we have a valid frame */
  217.                 hp->rcvbuf->cnt -= 2;           /* Toss 2 crc bytes */
  218.                 bp = alloc_mbuf(sizeof(struct phdr));
  219.                 bp->cnt = sizeof(struct phdr);            
  220.                 phdr = (struct phdr *)bp->data;
  221.                 phdr->type = TYPE_AX25;
  222.                 phdr->iface = hp->iface;
  223.                 bp->next = hp->rcvbuf;
  224.                 enqueue(&Hopper,bp);       /* queue it in */
  225.  
  226.                 /* packet queued - get buffer for next frame */
  227.                 hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  228.                 hp->rcp = hp->rcvbuf->data;
  229.                 hp->rcvbuf->cnt = 0;
  230.                 if(hp->rcvbuf == NULLBUF) {
  231.                     /* No memory, abort receiver */
  232.                     restore(i_state);
  233.                     printf("DISASTER! Out of Memory for Receive!\n");
  234.                     write_scc(CTL+base,R3,Rx8);
  235.                     return;
  236.                 }
  237.             } /* end good frame queued */
  238.         }  /* end check for active receive upon EOF */
  239.         hp->rstate = ACTIVE;    /* and clear error status */
  240.     } /* end EOF check */
  241.     restore(i_state);
  242. }
  243.  
  244. /* egchan transmit interrupt service routine
  245.  *
  246.  * The state variable tstate, along with some static pointers,
  247.  * represents the state of the transmit "process".
  248.  */
  249. static void
  250. egtxint(hp)
  251. register struct egchan *hp;
  252. {
  253.     register int16 base;
  254.     char i_state,c;
  255.  
  256.     i_state = dirps();
  257.  
  258.     if(hp->tstate != DEFER && hp->tstate)
  259.         hp->txints++;
  260.     base = hp->base;
  261.  
  262.     switch(hp->tstate) {
  263.     case FLAGOUT:
  264.         /* Here after CRC sent and Tx interrupt fires.
  265.          * To give the SCC a chance to get the FLAG
  266.          * out, we delay 100 Ms
  267.          */
  268.         hp->tstate = IDLE;    /* fall thru to IDLE */
  269.         waitmsec(10);        /* 100 msec wait for flag Tx */
  270.         /* Note, it may be possible to stuff out a
  271.          * meaningless character, wait for the interrupt
  272.          * then go to idle.  A delay is absolutely necessary
  273.          * here else the packet gets truncated prematurely
  274.          * when no other packet is waiting to be sent.
  275.          * IDLE state indicates either we are starting a brand new packet
  276.          * as a result of its being queued for transmission (egtxint called
  277.          * from eg_raw), or after a frame has been transmitted (as a
  278.          * result of a Tx buffer empty interrupt after the CRC/FLAG
  279.          */
  280.     case IDLE:
  281.         /* Transmitter idle. Find a frame for transmission */
  282.         if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF) {
  283.             /* Nothing to send - return to receive mode
  284.              * Tx OFF now - flag should have gone
  285.              */
  286.             rts(hp,OFF);
  287.             restore(i_state);
  288.             return;
  289.         }
  290.         /* If a buffer to send, we drop thru here */
  291.     case DEFER:        /* we may have deferred prev xmit attempt */
  292.         /* PPERSIST CALCULATION: we use the lower byte of the
  293.          * 8253 timer 0 count, as a random number (0-255).
  294.          * If the persist value is higher, wait one slot time
  295.          */
  296.         if(hp->params[PERSIST] >= peekb(0x40,0x6c))
  297.             waitmsec(hp->params[SLOTIME]);
  298.  
  299.         /* Check DCD so we don't step on a frame being received */
  300.         /* DCD is ACTIVE LOW on the SCC DCD pin, but the bit in R0 */
  301.         /* is SET when DCD is ACTIVE!! */
  302.  
  303.         if((read_scc(base+CTL,R0) & DCD) > 0) { /* Carrier Detected? */
  304.             hp->tstate = DEFER;    /* defer xmit */
  305.             /* don't release dequeued buffer...*/
  306.             restore(i_state);
  307.             return;
  308.         }
  309.  
  310.         rts(hp,ON);   /* Transmitter on */
  311.         /* ints not enabled yet */
  312.  
  313.         /* Get next char to send */
  314.         pullup(&hp->sndbuf,&c,1);        /* one char at a time */
  315.         write_scc(CTL+base,R0,RES_Tx_CRC);    /* reset for next frame */
  316.         outportb(base+DATA,c);        /* First char out now */
  317.  
  318.         /* select transmit interrupts to enable */
  319.  
  320.         write_scc(CTL+base,R15,TxUIE);        /* allow Underrun int only */
  321.         write_scc(CTL+base,R1,TxINT_ENAB|EXT_INT_ENAB);  /* Tx/Extern ints on */
  322.         write_scc(CTL+base,R9,MIE|NV);        /* master enable */
  323.         /* enable interrupt latch on board */
  324.         outportb(hp->dmactrl,INTENABLE);
  325.  
  326.         hp->tstate = ACTIVE;    /* char going out now */
  327.         restore(i_state);
  328.         return;
  329.  
  330.     case ACTIVE:
  331.         /* Here we are actively sending a frame */
  332.         if(pullup(&hp->sndbuf,&c,1) == 1) {
  333.             outportb(hp->base+DATA,c);    /* next char is gone */
  334.             /* stuffing a char satisfies Interrupt condition */
  335.         } else {
  336.             /* No more to send - just stop till underrun int */
  337.             hp->tstate = UNDERRUN;
  338.             free_p(hp->sndbuf);
  339.             /* now we reset the EOM latch & enable underrun int */
  340.             write_scc(CTL+base,R0,RES_EOM_L);    /* send CRC at underrun */
  341.             write_scc(CTL+hp->base,R0,RES_Tx_P); /* reset Tx Int Pend */
  342.         }
  343.         restore(i_state);
  344.         return;     /* back to wait for interrupt */
  345.  
  346.     case UNDERRUN:
  347.         /*
  348.          * This state is handled by an UNDERRUN interrupt, which
  349.          * is an External Status interrupt.  At UNDERRUN, the
  350.          * UNDERRUN/EOM latch in R0 will be 0, so the SCC will send
  351.          * CRC and ending flag.  After the CRC clears the Tx buffer,
  352.          * a TX BUFF EMPTY interrupt will fire.  At that time, we
  353.          * should be in FLAGOUT state, ready to send another frame
  354.          * if one is there to send.
  355.          */
  356.         break;
  357.     } /* end switch */
  358.     restore(i_state);
  359. }
  360.  
  361. /* SET Transmit or Receive Mode
  362.  * Set RTS (request-to-send) to modem on Transmit
  363.  */
  364. static void
  365. rts(hp,x)
  366. register struct egchan *hp;
  367. int16 x;
  368. {
  369. int16 tc;
  370. long br;
  371.  
  372.     /* Reprogram BRG and turn on transmitter to send flags */
  373.     if(x == ON) {                /* Turn Tx ON and Receive OFF */
  374.         write_scc(CTL+hp->base,R3,Rx8);    /* Rx off */
  375.         waitmsec(50);            /* 500 msec delay before on */
  376.         hp->rstate = IDLE;
  377.         write_scc(CTL+hp->base,R9,0);    /* Interrupts off */
  378.         br = hp->speed;         /* get desired speed */
  379.         tc = (XTAL/br)-2;        /* calc 1X BRG divisor */
  380.         write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  381.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  382.  
  383.         write_scc(CTL+hp->base,R5,TxCRC_ENAB|RTS|TxENAB|Tx8|DTR);
  384.         /* Transmitter now on */
  385.         write_scc(CTL+hp->base,R0,RES_Tx_CRC);/* CRC reset */
  386.         waitmsec(hp->params[TXDELAY]);      /* Delay after Tx on */
  387.     } else {    /* Tx OFF and Rx ON */
  388.         hp->tstate = IDLE;
  389.         write_scc(CTL+hp->base,R5,Tx8|DTR);     /* TX off now */
  390.         write_scc(CTL+hp->base,R0,ERR_RES);     /* reset error bits */
  391.  
  392.         write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  393.         write_scc(CTL+hp->base,R15,BRKIE);        /* allow ABORT int */
  394.  
  395.         /* delay for squelch tail before enable of Rx */
  396.         waitmsec(hp->params[SQUELDELAY]);    /* keep it up  */
  397.  
  398.         /* Reprogram BRG for 32x clock for receive DPLL */
  399.         write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  400.         br = hp->speed;             /* get desired speed */
  401.         tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  402.         write_scc(CTL+hp->base,R12,tc&0xFF);    /* lower byte */
  403.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF);    /* upper bite */
  404.         write_scc(CTL+hp->base,R14,BRSRC|SEARCH);    /* SEARCH mode, keep BRG source */
  405.         write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  406.  
  407.         /* Now, turn on the receiver and hunt for a flag */
  408.         write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  409.         hp->rstate = ACTIVE;            /* Normal state */
  410.     }
  411. }
  412.  
  413. /* Initialize eg controller parameters */
  414. static int
  415. egchanparam(hp)
  416. register struct egchan *hp;
  417. {
  418.     int16 tc;
  419.     long br;
  420.     char i_state;
  421.     register int16 base;
  422.  
  423.     /* Initialize 8530 channel for SDLC operation */
  424.  
  425.     base = hp->base;
  426. #ifdef    notdef
  427.     printf("Initializing Channel %c - Base = %x\n",base&2?'A':'B',base);
  428. #endif
  429.     i_state = dirps();
  430.  
  431.     switch(base & 2){
  432.     case 2:
  433.         write_scc(CTL+base,R9,CHRA);    /* Reset channel A */
  434.         break;
  435.     case 0:
  436.         write_scc(CTL+base,R9,CHRB);    /* Reset channel B */
  437.         break;
  438.     }
  439.  
  440.     /* Deselect all Rx and Tx interrupts */
  441.     write_scc(CTL+base,R1,0);
  442.  
  443.     /* Turn off external interrupts (like CTS/CD) */
  444.     write_scc(CTL+base,R15,0);
  445.  
  446.     /* X1 clock, SDLC mode */
  447.     write_scc(CTL+base,R4,SDLC|X1CLK);           /* SDLC mode and X1 clock */
  448.  
  449.     /* Now some misc Tx/Rx parameters */
  450.     /* CRC PRESET 1, NRZI Mode */
  451.     write_scc(CTL+base,R10,CRCPS|NRZI);
  452.  
  453.     /* Set up BRG and DPLL multiplexers */
  454.     /* Tx Clk from BRG. Rcv Clk from DPLL, TRxC pin outputs DPLL */
  455.     write_scc(CTL+base,R11,TCBR|RCDPLL|TRxCDP|TRxCOI);
  456.  
  457.     /* Null out SDLC start address */
  458.     write_scc(CTL+base,R6,0);
  459.  
  460.     /* SDLC flag */
  461.     write_scc(CTL+base,R7,FLAG);
  462.  
  463.     /* Set up the Transmitter but don't enable it */
  464.     /*  DTR, 8 bit TX chars only - TX NOT ENABLED */
  465.     write_scc(CTL+base,R5,Tx8|DTR);
  466.  
  467.     /* Receiver - intial setup only - more later */
  468.     write_scc(CTL+base,R3,Rx8);            /* 8 bits/char */
  469.  
  470.     /* Setting up BRG now - turn it off first */
  471.     write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  472.  
  473.     /* set the 32x time constant for the BRG in Receive mode */
  474.  
  475.     br = hp->speed;             /* get desired speed */
  476.     tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  477.  
  478.     write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  479.     write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  480.  
  481.     /* Time to set up clock control register for RECEIVE mode
  482.      * Eagle has xtal osc going to pclk at 3.6864 Mhz
  483.      * The BRG is sourced from that, and set to 32x clock
  484.      * The DPLL is sourced from the BRG, and feeds the TRxC pin
  485.      * Transmit clock & Receive clock come from DPLL
  486.      */
  487.  
  488.     /* Following subroutine sets up and ENABLES the receiver */
  489.     rts(hp,OFF);           /* TX OFF and RX ON */
  490.  
  491.     write_scc(CTL+hp->base,R14,BRSRC|SSBR);       /* DPLL from BRG, BRG source is PCLK */
  492.     write_scc(CTL+hp->base,R14,BRSRC|SEARCH);       /* SEARCH mode, keep BRG source */
  493.  
  494.     write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  495.  
  496.     /* enable the receive interrupts */
  497.  
  498.     write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  499.     write_scc(CTL+hp->base,R15,BRKIE);        /* ABORT int */
  500.     write_scc(CTL+hp->base,R9,MIE|NV);    /* master enable */
  501.  
  502.     /* enable interrupt latch on board */
  503.     outportb(hp->dmactrl,INTENABLE);
  504.  
  505.     /* Now, turn on the receiver and hunt for a flag */
  506.     write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  507.  
  508.     restore(i_state);
  509.     return 0;
  510. }
  511.  
  512. /* Attach a EAGLE interface to the system
  513.  * argv[0]: hardware type, must be "eagle"
  514.  * argv[1]: I/O address, e.g., "0x300"
  515.  * argv[2]: vector, e.g., "2"
  516.  * argv[3]: mode, must be:
  517.  *        "ax25" (AX.25 UI frame format)
  518.  * argv[4]: interface label, e.g., "eg0"
  519.  * argv[5]: receiver packet buffer size in bytes
  520.  * argv[6]: maximum transmission unit, bytes
  521.  * argv[7]: interface speed, e.g, "1200"
  522.  */
  523. int
  524. eg_attach(argc,argv)
  525. int argc;
  526. char *argv[];
  527. {
  528.     register struct iface *if_pca,*if_pcb;
  529.     struct egchan *hp;
  530.     int dev;
  531.  
  532.     /* Quick check to make sure args are good and mycall is set */
  533.     if(strcmp(argv[3],"ax25") != 0){
  534.         printf("Mode %s unknown for interface %s\r\n",
  535.             argv[3],argv[4]);
  536.         return -1;
  537.     }
  538.     if(if_lookup(argv[4]) != NULLIF){
  539.         printf("Interface %s already exists\n",argv[4]);
  540.         return -1;
  541.     }
  542.     axarp();
  543.     if(Mycall.call[0] == '\0'){
  544.         printf("set mycall first\r\n");
  545.         return -1;
  546.     }
  547.     /* Note: More than one card can be supported if you give up a COM:
  548.      * port, thus freeing up an IRQ line and port address
  549.      */
  550.  
  551.     if(Egnbr >= EGMAX) {
  552.         printf("Only 1 EAGLE controller supported right now!\r\n");
  553.         return -1;
  554.     }
  555.     dev = Egnbr++;
  556.  
  557.     /* Initialize hardware-level control structure */
  558.     Eagle[dev].addr = htoi(argv[1]);
  559.     Eagle[dev].vec = htoi(argv[2]);
  560.  
  561.     /* Save original interrupt vector */
  562.     Eagle[dev].oldvec = getirq(Eagle[dev].vec);
  563.  
  564.     /* Set new interrupt vector */
  565.     if(setirq(Eagle[dev].vec,eghandle[dev]) == -1){
  566.         printf("IRQ %u out of range\n",Eagle[dev].vec);
  567.         Egnbr--;
  568.         return -1;
  569.     }
  570.     /* Create interface structures and fill in details */
  571.     if_pca = (struct iface *)calloc(1,sizeof(struct iface));
  572.     if_pcb = (struct iface *)calloc(1,sizeof(struct iface));
  573.  
  574.     /* Append "a" to interface associated with A channel */
  575.     if_pca->name = malloc((unsigned)strlen(argv[4])+2);
  576.     strcpy(if_pca->name,argv[4]);
  577.     strcat(if_pca->name,"a");
  578.     /* Append "b" to iface associated with B channel */
  579.     if_pcb->name = malloc((unsigned)strlen(argv[4])+2);
  580.     strcpy(if_pcb->name,argv[4]);
  581.     strcat(if_pcb->name,"b");
  582.  
  583.     if_pcb->mtu = if_pca->mtu = atoi(argv[6]);
  584.     if_pcb->ioctl = if_pca->ioctl = eg_ctl;
  585.     if_pca->dev = 2*dev;            /* eg0a */
  586.     if_pcb->dev = 2*dev + 1;        /* eg0b */
  587.     if_pcb->stop = if_pca->stop = eg_stop;
  588.     if_pcb->output = if_pca->output = ax_output;
  589.     if_pcb->raw = if_pca->raw = eg_raw;
  590.  
  591.     if(strcmp(argv[3],"ax25") == 0) {
  592.         /* Must be true, was checked at top */
  593.         if_pcb->send = if_pca->send = ax_send;
  594.         if(if_pcb->hwaddr == NULLCHAR)
  595.             if_pcb->hwaddr = malloc(sizeof(Mycall));
  596.         memcpy(if_pcb->hwaddr,(char *)&Mycall,sizeof(Mycall));
  597.         if(if_pca->hwaddr == NULLCHAR)
  598.             if_pca->hwaddr = malloc(sizeof(Mycall));
  599.         memcpy(if_pca->hwaddr,(char *)&Mycall,sizeof(Mycall));
  600.     }
  601.     /* Link em in to the interface chain */
  602.     if_pca->next = if_pcb;
  603.     if_pcb->next = Ifaces;
  604.     Ifaces = if_pca;
  605.  
  606.     /* set params in egchan table for CHANNEL B */
  607.  
  608.     hp = &Egchan[2*dev+1];                /* eg1 is offset 1 */
  609.     hp->iface = if_pcb;
  610.     hp->stata = Eagle[dev].addr + CHANA + CTL;    /* permanent status */
  611.     hp->statb = Eagle[dev].addr + CHANB + CTL;    /* addrs for CHANA/B*/
  612.     hp->dmactrl = Eagle[dev].addr + DMACTRL;    /* Eagle control reg */
  613.     hp->speed = (int16)atoi(argv[7]);
  614.     hp->base = Eagle[dev].addr + CHANB;
  615.     hp->bufsiz = atoi(argv[5]);
  616.     hp->tstate = IDLE;
  617.     /* default KISS Params */
  618.     hp->params[TXDELAY] = 25;        /* 250 Ms */
  619.     hp->params[PERSIST] = 64;        /* 25% persistence */
  620.     hp->params[SLOTIME] = 10;        /* 100 Ms */
  621.     hp->params[SQUELDELAY] = 20;        /* 200 Ms */
  622.  
  623.     write_scc(CTL+hp->stata,R9,FHWRES);     /* Hardware reset */
  624.                         /* one time only */
  625.     /* Disable interrupts with Master interrupt ctrl reg */
  626.     write_scc(CTL+hp->stata,R9,0);
  627.  
  628.     egchanparam(hp);
  629.  
  630.     /* Pre-allocate a receive buffer */
  631.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  632.     if(hp->rcvbuf == NULLBUF) {
  633.         /* No memory, abort receiver */
  634.         printf("EGATTACH: No memory available for Receive buffers\n");
  635.         /* Restore original interrupt vector */
  636.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  637.         Egnbr--;
  638.         return(-1);
  639.     }
  640.     hp->rcp = hp->rcvbuf->data;
  641.     hp->rcvbuf->cnt = 0;
  642.     hp->sndq = NULLBUF;
  643.  
  644.     /* set params in egchan table for CHANNEL A */
  645.     hp = &Egchan[2*dev];            /* eg0a is offset 0 */
  646.     hp->iface = if_pca;
  647.     hp->speed = (int16)atoi(argv[7]);
  648.     hp->base = Eagle[dev].addr + CHANA;
  649.     hp->bufsiz = atoi(argv[5]);
  650.     hp->tstate = IDLE;
  651.     /* default KISS Params */
  652.     hp->params[TXDELAY] = 25;        /* 250 Ms */
  653.     hp->params[PERSIST] = 64;        /* 25% persistence */
  654.     hp->params[SLOTIME] = 10;        /* 100 Ms */
  655.     hp->params[SQUELDELAY] = 20;        /* 200 Ms */
  656.  
  657.     egchanparam(hp);
  658.  
  659.     /* Pre-allocate a receive buffer */
  660.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  661.     if(hp->rcvbuf == NULLBUF) {
  662.         /* No memory, abort receiver */
  663.         printf("EGATTACH: No memory available for Receive buffers\n");
  664.         /* Restore original interrupt vector */
  665.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  666.         Egnbr--;
  667.         return -1;
  668.     }
  669.     hp->rcp = hp->rcvbuf->data;
  670.     hp->rcvbuf->cnt = 0;
  671.     hp->sndq = NULLBUF;
  672.  
  673.     write_scc(CTL+hp->base,R9,MIE|NV);        /* master interrupt enable */
  674.  
  675.     /* Enable interrupts on the EAGLE card itself */
  676.     outportb(hp->dmactrl,INTENABLE);
  677.  
  678.     /* Enable interrupt */
  679.     maskon(Eagle[dev].vec);
  680.  
  681.     return 0;
  682. }
  683.  
  684.  
  685. /* Shut down interface */
  686. int
  687. eg_stop(iface)
  688. struct iface *iface;
  689. {
  690.     int16 dev;
  691.  
  692.     dev = iface->dev;
  693.     if(dev & 1)
  694.         return 0;
  695.     dev >>= 1;    /* Convert back into eagle number */
  696.  
  697.     /* Turn off interrupts */
  698.     maskoff(Eagle[dev].vec);
  699.  
  700.     /* Restore original interrupt vector */
  701.     setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  702.  
  703.     /* Force hardware reset */
  704.     write_scc(CTL+Eagle[dev].addr + CHANA,R9,FHWRES);
  705.  
  706.     /* resets interrupt enable on eagle card itself */
  707.     outportb(Eagle[dev].addr+DMACTRL,0);
  708.     return 0;
  709. }
  710.  
  711. /* Send raw packet on eagle card */
  712. int
  713. eg_raw(iface,bp)
  714. struct iface *iface;
  715. struct mbuf *bp;
  716. {
  717.     char kickflag;
  718.     struct egchan *hp;
  719.  
  720.     dump(iface,IF_TRACE_OUT,TYPE_AX25,bp);
  721.     hp = &Egchan[iface->dev];
  722.     kickflag = (hp->sndq == NULLBUF) & (hp->sndbuf == NULLBUF);    /* clever! flag=1 if something in queue */
  723.     enqueue(&hp->sndq,bp);
  724.     if(kickflag)            /* simulate interrupt to xmit */
  725.         egtxint(hp);        /* process interrupt */
  726.     return 0;
  727. }
  728. /* routine to delay n increments of 10 milliseconds
  729.  * about right on a turbo XT - will be slow on 4.77
  730.  * Still looking for a way to use the 8253 timer...
  731.  */
  732. void
  733. waitmsec(n)
  734. int n;
  735. {
  736.     long i;
  737.  
  738.     for(i=0L; i < (200L*n); i++)
  739.         ;  /* simple loop delay */
  740. }
  741.  
  742. /* display EAGLE Channel stats */
  743. int
  744. doegstat()
  745. {
  746.     struct egchan *hp0, *hp1;
  747.     int i;
  748.  
  749.     for(i=0; i<EGMAX; i++) {
  750.         hp0 = &Egchan[i];
  751.         hp1 = &Egchan[i+1];
  752.  
  753.         printf("EAGLE Board Statistics:\n\n");
  754.         printf("Base Addr\tRxints\tTxints\tExints\tEnqued\tCrcerr\tAborts\tRxOvers\tRFrames\n");
  755.         printf("---------\t------\t------\t------\t------\t------\t------\t-------\t-------\n");
  756.         printf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n", hp0->base, hp0->rxints,
  757.             hp0->txints, hp0->exints, hp0->enqueued, hp0->crcerr, hp0->aborts,
  758.             hp0->rovers,hp0->rxframes,
  759.             hp0->rstate==0?"IDLE":hp0->rstate==1?"ACTIVE":hp0->rstate==2?"RXERROR":hp0->rstate==3?"RXABORT":"TOOBIG");
  760.  
  761.         printf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n\n", hp1->base, hp1->rxints,
  762.             hp1->txints, hp1->exints, hp1->enqueued, hp1->crcerr, hp1->aborts,
  763.             hp1->rovers,hp1->rxframes,
  764.             hp1->rstate==0?"IDLE":hp1->rstate==1?"ACTIVE":hp1->rstate==2?"RXERROR":hp1->rstate==3?"RXABORT":"TOOBIG");
  765.     }
  766.     return 0;
  767. }
  768.  
  769. /* Subroutine to set kiss params in channel tables */
  770. int
  771. eg_ctl(iface,argc,argv)
  772. struct iface *iface;
  773. int argc;
  774. char *argv[];
  775. {
  776.     struct egchan *hp;
  777.     int p, v;
  778.  
  779.     if(argc < 2){
  780.         printf("Insufficient parameters\r\n");
  781.         return 1;
  782.     }
  783.     hp = &Egchan[iface->dev];        /* point to channel table */
  784.     p = atoi(argv[0]);          /* parameter in binary */
  785.     if(p > 3){
  786.         printf("parameter %d out of range\r\n",p);
  787.         return 1;
  788.     }
  789.     v = atoi(argv[1]);          /* value to be loaded */
  790.     hp->params[p] = v;          /* Stuff in Kiss array */
  791.     return 0;
  792. }
  793.